<script setup>
import { onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'
import { io } from 'socket.io-client'
const userName = ref('')
const userList = ref([])
const router = useRouter()
const customGenerationFunction = () =>
  (Math.random().toString(36) + '0000000000000000000').substring(2, 16)

onMounted(() => {
  if (!sessionStorage.getItem('userId')) {
    sessionStorage.setItem('userId', customGenerationFunction())
  }
})
const goHome = () => {
  router.push('/')
}
let socket = null
const createWs = () => {
  socket = io('http://192.168.137.130:8090', {
    query: {
      userName: userName.value,
      userId: sessionStorage.getItem('userId')
    }
  })
  socket.on('connect', () => {
    console.log('链接成功...')
  })
  socket.on('disconnect', () => {})
  // 新增用户
  socket.on('add-user', (data) => {
    console.log(data)
    userList.value = data
    console.log(userList.value)
  })
  // 监听用户发送的消息
  socket.on('message', (data) => {
    if (data.type === 'user') {
      console.log(data)
    }
  })
}
const sendMessage = () => {
  socket.emit('hello', userName.value)
}
/**
 * 发送消息
 * @param {} user
 */
const sendMessageToUser = (user) => {
  socket.emit('sendToUser', {
    userId: user.userId,
    message: 'hello world'
  })
}
</script>
<template>
  <div class="home">
    <div class="header">
      <div class="back-box" @click="goHome">主页</div>
      peerjs音视频示例
    </div>
    <div class="content">
      <div class="left">
        <div class="title">摄像头</div>
      </div>
      <div class="center">
        <el-input v-model="userName" placeholder="用户名"></el-input>
        <el-button @click="createWs" type="primary">连接</el-button>
        <el-button @click="sendMessage">发送消息</el-button>
      </div>
      <div class="right">
        <div class="title">用户列表</div>
        <ul>
          <li v-for="item in userList" :key="item.userId" class="flex p-2 justify-between">
            <span>{{ item.userName }} {{ item.userId }}</span>
            <el-button size="small" type="primary" @click="sendMessageToUser(item)">发送</el-button>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>
<style scoped lang="scss">
.home {
  width: 100vw;
  height: 100vh;
  background-color: #1e2831;
  color: #f5f5f5;
  font-size: 14px;
  .header {
    height: 50px;
    width: 100%;
    background-color: rgb(48, 64, 81);
    text-align: center;
    line-height: 50px;
    .back-box {
      position: absolute;
      height: 50px;
      padding: 0 10px;
      cursor: pointer;
    }
  }
  .content {
    height: calc(100% - 50px);
    padding: 5px;
    display: flex;
    .title {
      background-color: rgb(48, 64, 81);
      height: 35px;
      line-height: 35px;
      text-align: center;
      font-size: 12px;
    }
    .left {
      height: 100%;
      width: 300px;
      background-color: #28333d;
    }
    .center {
      margin: 0 5px;
      flex: 1;
      background-color: #28333d;
    }
    .right {
      width: 300px;
      background-color: #28333d;
    }
  }
  .box {
    width: 150px;
    height: 150px;
    border-radius: 4px;
    margin: 0 20px;
    line-height: 150px;
    text-align: center;
    background-color: #28333d;
    cursor: pointer;
    &:hover {
      background-color: rgb(48, 64, 81);
    }
  }
}
</style>
